package cn.mw.cmdb.controller;

import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.enums.OperatorTypeDescEnum;
import cn.mw.cmdb.param.GroupSearchParam;
import cn.mw.cmdb.service.ServiceAction;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.ModelGroupServiceImpl;
import cn.mw.cmdb.view.ViewManager;
import cn.mw.log.client.LogClientSimple;
import cn.mw.log.enums.BusinessLogType;
import cn.mw.log.enums.LogLevel;
import cn.mw.microMonitorCommon.api.cmdb.TreeView;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import cn.mw.microMonitorCommon.constant.Constants;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import cn.mw.microMonitorCommon.util.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static cn.mw.cmdb.enums.PageDescEnum.ModelGroup;

@RequestMapping("/groupManage")
@Controller
@Tag(name = "分组管理")
@Slf4j
public class GroupController extends BaseApiService {

    private CmdbServiceManage cmdbServiceManage;

    private MessageSource messageSource;

    private ViewManager viewManager;

    private LogClientSimple logClientSimple;

    public GroupController(CmdbServiceManage cmdbServiceManage, MessageSource messageSource, ViewManager viewManager, LogClientSimple logClientSimple) {
        this.cmdbServiceManage = cmdbServiceManage;
        this.messageSource = messageSource;
        this.viewManager = viewManager;
        this.logClientSimple = logClientSimple;
    }

    @PostMapping("/addGroup")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('285')")
    @Operation(summary = "新增分组")
    public ResponseBase addGroup(@RequestBody @Validated ModelGroupInfo param) throws Exception {
        Object ret = cmdbServiceManage.service(param, ServiceAction.add);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.add.getCname() + ModelGroup.getDesc(),
                        OperatorTypeDescEnum.add.getCname() + param.getName(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(ret);
    }

    @PostMapping("/updGroup")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('286')")
    @Operation(summary = "更新分组")
    public ResponseBase updGroup(@RequestBody ModelGroupInfo param) throws Exception {
        cmdbServiceManage.service(param, ServiceAction.update);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.update.getCname() + ModelGroup.getDesc(),
                        OperatorTypeDescEnum.update.getCname() + param.getName(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("upd-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/delGroup")
    @ResponseBody
    @PreAuthorize("@mwAuthorizationCheck.check('287')")
    @Operation(summary = "删除分组")
    public ResponseBase delGroup(@RequestBody ModelGroupInfo param) throws Exception {
        cmdbServiceManage.service(param, ServiceAction.remove);
        String nickName = SecurityUtils.getUser().getNickName();
        logClientSimple.addBusinessLog(LogLevel.INFO.getId(), BusinessLogType.CMDB_MANAGE.getId(),
                messageSource.getMessage("mwCmdb-operation-log", new Object[]{nickName, DateUtils.now()
                        , BusinessLogType.CMDB_MANAGE.getCnName(), OperatorTypeDescEnum.remove.getCname() + ModelGroup.getDesc(),
                        OperatorTypeDescEnum.remove.getCname() + param.getName(), Constants.OperationStatus.SUCCESS}, LocaleContextHolder.getLocale()));

        return setResultSuccess(messageSource.getMessage("del-success", null, LocaleContextHolder.getLocale()));
    }

    @PostMapping("/selGroup")
    @ResponseBody
    @Operation(summary = "查看分组")
    public ResponseBase selGroup(@RequestBody ModelGroupInfo param) throws Exception {
        Object ret = cmdbServiceManage.service(param, ServiceAction.select);
        return setResultSuccess(ret);
    }

    @PostMapping("/listGroup")
    @ResponseBody
    @Operation(summary = "查看分组列表")
    public ResponseBase listGroup(@RequestBody GroupSearchParam param) throws Exception {
        Object ret = cmdbServiceManage.service(param, ServiceAction.list);
        return setResultSuccess(ret);
    }

    @PostMapping("/listGroupTree")
    @ResponseBody
    @Operation(summary = "查看分组树")
    public ResponseBase listGroupTree(@RequestBody GroupSearchParam param) throws Exception {
        ModelGroupServiceImpl service = (ModelGroupServiceImpl) cmdbServiceManage.getModelService(ModelGroupServiceImpl.ID);
        List<TreeView> list = service.getTreeList(param);
        Object ret = viewManager.getTreeViewObject(list, null);
        return setResultSuccess(ret);
    }

    @PostMapping("/syncMonitorGroupInfo")
    @ResponseBody
    @Operation(summary = "同步监控分组信息")
    public ResponseBase syncMonitorGroupInfo() throws Exception {
        ModelGroupServiceImpl service = (ModelGroupServiceImpl) cmdbServiceManage.getModelService(ModelGroupServiceImpl.ID);
        service.syncGroupInfoFromMoniterServer();
        return setResultSuccess(messageSource.getMessage("upd-success", null, LocaleContextHolder.getLocale()));
    }
}
